<template>
  <div class="page">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="项目名称/模块名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      row-key="id"
      style="width: 100%;">
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label=""
        width="50"
        type="expand">
         <template slot-scope="props">
           <div>
               <div v-if="props.row.frontends" style="width:100%;height: 80px;border:1px dashed #d4d4d4;display: flex;">
                  <div style="line-height: 80px;margin-left:20px;width:100px;text-align: center;">展示层</div>
                  <div>
                    <div v-for="(item,index) in props.row.frontends" style="float:left;width:400px;display: flex;height: 60px;margin:10px;border:1px solid #d4d4d4;">
                      <icon-svg style="padding:10px;width:60px;height:60px;border-right:1px solid #d4d4d4;" v-if="item.framework==='vue'" name="vue"></icon-svg>
                      <div style="margin:3px 1px 0px 0px;width:380px;">
                          <div style="height:20px;text-align:center;">{{item.name}}</div>
                          <div style="display: flex;">
                            <div style="width:50%;height:30px;">
                               <div v-if="item.runtimeStatus" class="dot" style="background-color: #0b9f15;"></div>
                               <div v-else class="dot" style="background-color: #cc0003;"></div>
                            </div>
                            <div style="width:50%;height:30px;margin-right: 10px;text-align:right;">
                               <el-button type="text" size="middle" @click="toPublishFront(item)">发布</el-button>
                               <el-button type="text" size="middle" @click="toConfigFront(item)">配置</el-button>
                               <el-button type="text" size="middle" @click="toRestartFront(item)">重启</el-button>
                            </div>
                          </div>
                      </div>
                    </div>
                  </div>
              </div>
              <div v-if="props.row.frontends&&props.row.backend" style="width: 100%;text-align: center;margin: 10px 0;">
                  <icon-svg name="douarrow"></icon-svg>
              </div>
              <div v-if="props.row.backend" style="width:100%;border:1px dashed #d4d4d4;display: flex;">
                 <div style="line-height: 80px;margin-left:20px;width:100px;text-align: center;">逻辑层</div>
                 <div>
                   <div style="width:100%">
                    <div v-for="(item,index) in props.row.backend.modules"  v-if="item.moduleType=='gateway'" style="display:flex;border:1px solid #d4d4d4;height:60px;width:800px;margin:10px;">
                      <icon-svg style="padding:10px;width:60px;height:60px;border-right:1px solid #d4d4d4;" name="springboot"></icon-svg>
                      <div style="margin:3px 1px 0px 0px;width:740px;text-align:center;">
                          <div style="height:20px;text-align:center;">{{item.name}}</div>
                          <div style="display: flex;">
                            <div style="width:30%;height:30px;">
                               <div v-if="item.runtimeStatus" class="dot" style="background-color: #0b9f15;"></div>
                               <div v-else class="dot" style="background-color: #cc0003;"></div>
                            </div>
                            <div style="width:70%;height:30px;margin-right: 10px;text-align:right;">
                               <el-button type="text" size="middle" @click="toPublishFront(item)">发布</el-button>
                               <el-button type="text" size="middle" @click="toConfigFront(item)">配置</el-button>
                               <el-button type="text" size="middle" @click="toRestartBackend(item)">重启</el-button>
                               <el-button type="text" size="middle" @click="toRuntimeLog(item)">日志</el-button>
                            </div>
                          </div>
                      </div>
                    </div>
                    <div style="clear:both;"></div>
                    </div>
                    <div style="width:100%">
                      <div v-for="(item,index) in props.row.backend.modules" style="float:left;width:350px;">
                        <div style="border:1px solid #d4d4d4;height: 60px;margin:10px;display:flex;" v-if="item.moduleType==='springboot'">
                          <icon-svg style="padding:10px;width:60px;height:60px;border-right:1px solid #d4d4d4;" name="springboot"></icon-svg>
                          <div style="margin:3px 1px 0px 0px;width:100%;text-align:center;">
                              <div style="margin:3px 1px 0px 0px;width:100%;text-align:center;">
                                  <div style="height:20px;text-align:center;">{{item.name}}</div>
                                  <div style="width:100%;display: flex;">
                                    <div style="width:30%;height:30px;">
                                       <div v-if="item.runtimeStatus" class="dot" style="background-color: #0b9f15;"></div>
                                       <div v-else class="dot" style="background-color: #cc0003;"></div>
                                    </div>
                                    <div style="width:70%;height:30px;margin-right: 10px;text-align:right;">
                                       <el-button type="text" size="middle" @click="toPublishFront(item)">发布</el-button>
                                       <el-button type="text" size="middle" @click="toConfigFront(item)">配置</el-button>
                                       <el-button type="text" size="middle" @click="toRestartBackend(item)">重启</el-button>
                                       <el-button type="text" size="middle" @click="toRuntimeLog(item)">日志</el-button>
                                    </div>
                                  </div>
                              </div>
                          </div>
                        </div>
                      </div>
                      <div style="clear:both;"></div>
                    </div>
                    <div v-for="(item,index) in props.row.backend.modules">
                      <div style="width:500px;border:1px solid #d4d4d4;height: 60px;margin:10px;display:flex;" v-if="item.moduleType==='eureka'">
                        <icon-svg style="padding:10px;width:60px;height:60px;border-right:1px solid #d4d4d4;" name="springboot"></icon-svg>
                        <div style="margin:3px 1px 0px 0px;width:100%;text-align:center;">
                            <div style="height:20px;text-align:center;">{{item.name}}</div>
                            <div style="display: flex;">
                              <div style="width:30%;height:30px;">
                                 <div v-if="item.runtimeStatus" class="dot" style="background-color: #0b9f15;"></div>
                                 <div v-else class="dot" style="background-color: #cc0003;"></div>
                              </div>
                              <div style="width:70%;height:30px;margin-right: 10px;text-align:right;">
                                 <el-button type="text" size="middle" @click="toPublishFront(item)">发布</el-button>
                                 <el-button type="text" size="middle" @click="toConfigFront(item)">配置</el-button>
                                 <el-button type="text" size="middle" @click="toRestartBackend(item)">重启</el-button>
                                 <el-button type="text" size="middle" @click="toRuntimeLog(item)">日志</el-button>
                              </div>
                            </div>
                        </div>
                      </div>
                    </div>
                    <div>
                      <div v-for="(item,index) in props.row.backend.modules" style="float:left;width:350px;">
                        <div style="border:1px solid #d4d4d4;height: 60px;margin:10px;display:flex;" v-if="item.moduleType==='tomcat'">
                          <icon-svg style="padding:10px;width:60px;height:60px;border-right:1px solid #d4d4d4;" name="tomcat"></icon-svg>
                          <div style="margin:3px 1px 0px 0px;width:100%;text-align:center;">
                              <div style="margin:3px 1px 0px 0px;width:100%;text-align:center;">
                                  <div style="height:20px;text-align:center;">{{item.name}}</div>
                                  <div style="display: flex;">
                                    <div style="width:30%;height:30px;">
                                       <div v-if="item.runtimeStatus" class="dot" style="background-color: #0b9f15;"></div>
                                       <div v-else class="dot" style="background-color: #cc0003;"></div>
                                    </div>
                                    <div style="width:70%;height:30px;margin-right: 10px;text-align:right;">
                                       <el-button type="text" size="middle" @click="toPublishFront(item)">发布</el-button>
                                       <el-button type="text" size="middle" @click="toConfigFront(item)">配置</el-button>
                                       <el-button type="text" size="middle" @click="toRestartBackend(item)">重启</el-button>
                                       <el-button type="text" size="middle" @click="toRuntimeLog(item)">日志</el-button>
                                    </div>
                                  </div>
                              </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
              </div>
              <div v-if="props.row.backend&&props.row.datasources" style="width: 100%;text-align: center;margin: 10px 0;">
                  <icon-svg name="douarrow"></icon-svg>
              </div>
              <div v-if="props.row.datasources" style="width:100%;height: 80px;border:1px dashed #d4d4d4;display: flex;">
                  <div style="line-height: 80px;margin-left:20px;width:100px;text-align: center;">存储层</div>
                  <div style="width:100%;">
                    <div v-for="(item,index) in props.row.datasources" style="float:left;">
                      <div  style="width:300px;border:1px solid #d4d4d4;height: 60px;margin:10px;display:flex;">
                        <icon-svg style="padding:10px;width:60px;height:60px;border-right:1px solid #d4d4d4;" :name="item.datasourceType"></icon-svg>
                        <div style="margin:3px 1px 0px 0px;width:240px;text-align:center;">
                            <div style="height:20px;text-align:center;">{{item.name}}</div>
                            <div style="display: flex;">
                              <div style="width:50%;height:30px;">
                                 <div v-if="item.runtimeStatus" class="dot" style="background-color: #0b9f15;"></div>
                                 <div v-else class="dot" style="background-color: #cc0003;"></div>
                              </div>
                              <div style="width:50%;height:30px;margin-right: 10px;text-align:right;">
                                 <el-button type="text" size="middle" @click="toViewPage(item,props.row.id)">查看</el-button>
                                 <el-button type="text" size="middle" @click="toRestartSources(item)">重启</el-button>
                              </div>
                            </div>
                        </div>
                      </div>
                    </div>
                  </div>
              </div>
            </div>
         </template>
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="left"
        label="项目名称">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="100"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="projectConfig(scope.row)">配置</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <project-publish v-if="publishWebVisible" ref="projectPublish"></project-publish>
    <config-nginx v-if="ConfigNginxVisible" ref="configNginx"></config-nginx>
    <data-view v-if="dataViewVisible" ref="dataView"></data-view>
  </div>
</template>

<script>
  import projectPublish from './project-publish'
  import configNginx from './config-nginx'
  import dataView from './data-view'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        publishWebVisible:false,
        ConfigNginxVisible:false,
        dataViewVisible:false,
      }
    },
    components: {
        projectPublish,
        configNginx,
        dataView,
    },
    activated () {
      this.getDataList()
    },
    methods: {
      toPublishFront(frontInfo){
        this.publishWebVisible = true
        this.$nextTick(() => {
          this.$refs.projectPublish.init(frontInfo)
        })
      },
      toViewPage(frontInfo,id){
        console.log(id)
        this.dataViewVisible = true
        this.$nextTick(() => {
          this.$refs.dataView.init(frontInfo,id)
        })
      },
      toConfigFront(frontInfo){
        this.ConfigNginxVisible = true
        this.$nextTick(() => {
          this.$refs.configNginx.init(frontInfo)
        })
      },
      toRestartBackend(item){
        this.$confirm(`确定对${item.name}进行重启操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/publish/restartBackend'),
            method: 'post',
             params: this.$http.adornParams({'id':item.id})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      toRestartSources(item){
        this.$confirm(`确定对${item.name}进行重启操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/publish/restartSources'),
            method: 'post',
             params: this.$http.adornParams({'id':item.id})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      toRestartFront(frontInfo){
        this.$confirm(`确定对${frontInfo.name}进行重启操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/publish/restartFront'),
            method: 'post',
             params: this.$http.adornParams({'id':frontInfo.id})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/project/treelist'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },


    }
  }
</script>
<style>
  .dot {
    margin-top: 16px;margin-left: 10px;
    width: 10px;
    height: 10px;
    border-radius: 100%;
    background: #ffff;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .2);

  }
</style>
